package com.yc.clouduser.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.clouduser.entity.User;
import com.yc.clouduser.mapper.UserMapper;
import com.yc.vcode.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzk
 * @since 2024-09-20
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping("getimage")
    String getimage(String uid){
        User user = userMapper.selectById(uid);
        return user.getPic();
    }

    @RequestMapping("byselect")
    Page<User> byselect(@RequestParam(defaultValue = "1") int pagenum, String uname, String email, String status){
        Page<User> page = new Page<>(pagenum, 4);

        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        //添加条件

        if(!"".equals(uname)){
            lqw.like(User::getName, uname);
        }
        if(!"".equals(email)){
            lqw.like(User::getEmail, email);
        }
        if(!"全部".equals(status)){
            lqw.eq(User::getStatus, status);
        }
        return userMapper.selectPage(page, lqw);
    }

    @RequestMapping("ban")
    Result ban(String uid){
        User user = userMapper.selectById(uid);
        user.setStatus("封禁");
        userMapper.updateById(user);
        return new Result(1, "封禁成功", null);
    }

    @RequestMapping("noban")
    Result noban(String uid){
        User user = userMapper.selectById(uid);
        user.setStatus("用户");
        userMapper.updateById(user);
        return new Result(1, "解禁成功", null);
    }

    @RequestMapping("cz")
    Result cz(String uid){
        User user = userMapper.selectById(uid);
        String md5 = DigestUtils.md5DigestAsHex("aaaa".getBytes());
        user.setPwd(md5);
        userMapper.updateById(user);
        return new Result(1, "重置密码成功", null);
    }

    @RequestMapping("updateUserData")
    Result updateUserData(String uid, String name, String email, String pic){
        User user = userMapper.selectById(uid);
        user.setName(name);
        user.setEmail(email);
        if(!"".equals(pic)){
            user.setPic(pic);
        }
        userMapper.updateById(user);
        return new Result(1, "修改信息成功", null);
    }

    @RequestMapping("getnum")
    Result getnum(){
        Long aLong = userMapper.selectCount(null);
        return new Result(1, "成功", aLong);
    }

}

